package cn.com.qiter.oss.controller;

import cn.com.qiter.common.Result;
import cn.com.qiter.oss.service.OssService;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;

@RestController
@RequestMapping("/eduoss/wxmini")
public class WxMiniOssController {
    private static final Log log = LogFactory.get();

    @Autowired
    private OssService ossService;

    @PostMapping(value = "/uploadVideo")
    public Result uploadVideo(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        String url = "";
        //检查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)){
            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;

            MultipartFile multipartFile = req.getFile("tempVideoPath");
            url = ossService.uploadFileAvatar(multipartFile);
            String openId = req.getParameter("openid");
            String tempVideoPath = req.getParameter("tempVideoPath");
            String tempThumbPath = req.getParameter("tempThumbPath");
            String upload = req.getParameter("upload");
            String json = req.getParameter("json");

        }

        return Result.ok().data("url",url);
    }

    @PostMapping(value = "/uploadThumb")
    public Result uploadThumb(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        String url = "";
        //检查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)){
            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;

            MultipartFile multipartFile = req.getFile("tempThumbPath");
            url = ossService.uploadFileAvatar(multipartFile);
            String openId = req.getParameter("openid");
            String tempVideoPath = req.getParameter("tempVideoPath");
            String tempThumbPath = req.getParameter("tempThumbPath");
            String upload = req.getParameter("upload");
            String json = req.getParameter("json");

        }

        return Result.ok().data("url",url);
    }

    @PostMapping(value = "/upload")
    public Result upload(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        String url = "";
        //检查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)){
            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;

            MultipartFile multipartFile = req.getFile("file");
            url = ossService.uploadFileAvatar(multipartFile);
            String openId = req.getParameter("openid");
            String tempVideoPath = req.getParameter("tempVideoPath");
            String tempThumbPath = req.getParameter("tempThumbPath");
            String upload = req.getParameter("upload");
            String json = req.getParameter("json");

        }

        return Result.ok().data("url",url);
    }

    @PostMapping(value = "/fileUpload")
    public ResponseEntity fileUpload(HttpServletRequest request) {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)){
            MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;

            //获取multiRequest 中所有的文件名
            Iterator iter=req.getFileNames();
            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=req.getFile(iter.next().toString());
                if(file!=null)
                {
                    String path="E:/imgUpload/"+file.getOriginalFilename();
                    //上传
                    log.debug(path, Level.DEBUG);
//                    file.transferTo(new File(path));
//                    files.setFileName(file.getOriginalFilename());//设置文件名 后期改为具体路径？
//                    //文件处理类，在数据库中保存文件路径
//                    filesService.save(files);
                }

            }

            MultipartFile multipartFile = req.getFile("file");

            String openId = req.getParameter("openid");
            String tempVideoPath = req.getParameter("tempVideoPath");
            String tempThumbPath = req.getParameter("tempThumbPath");
            String upload = req.getParameter("upload");
            String json = req.getParameter("json");

        }

        return ResponseEntity.ok(Result.ok());
    }
}
